package com.zycfc.zsf.boot.logback.util;

import org.slf4j.*;
import com.zycfc.zsf.boot.common.context.*;
import com.zycfc.zsf.boot.util.text.*;

public class TraceUtil
{
    public static final String ZYCFC_TRACE_ID = "ZYCFC-TRACE-ID";
    public static final String LOCAL_TRANCE_ID = "LOCAL-TRACE-ID";
    public static final String LOCAL_SPAN_ID = "LOCAL-SPAN-ID";
    public static final String LOCAL_SPAN_PARENT_ID = "LOCAL-SPAN-PARENT-ID";
    public static final String TRACE_LEVEL = "traceLevel";
    public static final String APPLICATION_NAME = "applicationName";
    public static final String SERVER_ENV = "serverEnv";
    
    public static String getZycfcTraceKeyName() {
        return "ZYCFC-TRACE-ID";
    }
    
    public static String getLocalTraceKeyName() {
        return "LOCAL-TRACE-ID";
    }
    
    public static String getApplicationNameName() {
        return "applicationName";
    }
    
    public static String getServerEnvName() {
        return "serverEnv";
    }
    
    public static String getTraceLevelName() {
        return "traceLevel";
    }
    
    public static void begin(final String key, final String value) {
        MDC.put(key, value);
    }
    
    public static void beginTrace(final String zycfcTraceId, final String localTraceId) {
        beginZycfcTrace(zycfcTraceId);
        beginLocalTrace(localTraceId);
    }
    
    public static void beginZycfcTrace(final String zycfcTraceId) {
        MDC.put("ZYCFC-TRACE-ID", zycfcTraceId);
    }
    
    public static void beginLocalTrace(final String localTraceId) {
        MDC.put("LOCAL-TRACE-ID", localTraceId);
    }
    
    public static void beginApplicationNameAndEnv() {
        beginApplicationNameAndEnv(ZsfEnvConstants.getApplicationName(), ZsfEnvConstants.getProfilesActive());
    }
    
    public static void beginApplicationNameAndEnv(final String applicationName, final String serverEnv) {
        MDC.put("applicationName", applicationName);
        MDC.put("serverEnv", serverEnv);
    }
    
    public static String beginTraceLevel(final String traceLevel) {
        int level = 0;
        if (!StringUtils.isEmpty(traceLevel)) {
            level = Integer.parseInt(traceLevel);
        }
        final String newTraceLevel = String.valueOf(level);
        MDC.put("traceLevel", newTraceLevel);
        return newTraceLevel;
    }
    
    public static String beginTraceLevelAddOne(final String traceLevel) {
        int level = 0;
        if (!StringUtils.isEmpty(traceLevel)) {
            level = Integer.parseInt(traceLevel) + 1;
        }
        final String newTraceLevel = String.valueOf(level);
        MDC.put("traceLevel", newTraceLevel);
        return newTraceLevel;
    }
    
    public static String get(final String key) {
        return MDC.get(key);
    }
    
    public static String getZycfcTrace() {
        return MDC.get("ZYCFC-TRACE-ID");
    }
    
    public static String getLocalTrace() {
        return MDC.get("LOCAL-TRACE-ID");
    }
    
    public static String getTraceLevel() {
        return MDC.get("traceLevel");
    }
    
    public static String getApplilcatonName() {
        return MDC.get("applicationName");
    }
    
    public static String getServerEnv() {
        return MDC.get("serverEnv");
    }
    
    public static void endTrace() {
        MDC.remove("ZYCFC-TRACE-ID");
        MDC.remove("LOCAL-TRACE-ID");
        MDC.remove("LOCAL-SPAN-ID");
        MDC.remove("LOCAL-SPAN-PARENT-ID");
    }
    
    @Deprecated
    public static void beginApplilcatonName() {
        beginApplicationNameAndEnv();
    }
    
    @Deprecated
    public static void beginApplilcatonName(final String applicationName, final String serverEnv) {
        beginApplicationNameAndEnv(applicationName, serverEnv);
    }
}
